<?php
/**
 * Created by PhpStorm.
 * User: xiongrui
 * Date: 2017-08-21
 * Time: 22:13
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\Order as OrderModel;
use app\common\model\OrderShoplist;

class QiyeOrder extends Admin
{

    public function index(){
      $this->getListBystatus();
      return $this->fetch();
    }
    public function wzf(){
        $this->getListBystatus(0, '未支付订单');
        return $this->fetch();
    }
    public function dfh(){
      $this->getListBystatus(1, '待发货');
      return $this->fetch();
    }
    public function dps(){
      $this->getListBystatus(2, '待配送');
      return $this->fetch();
    }
    public function dqs(){
      $this->getListBystatus(3, '待签收');
      return $this->fetch();
    }
    public function ywc(){
      $this->getListBystatus(4, '已完成');
      return $this->fetch();
    }

    public function getListBystatus($status = null, $title = '全部订单'){
        $map = $this->createMap();
        if($status !== null){
            $map['buystatus']  = $status;
        }
        $count = OrderModel::where($map)->count();
        $list = OrderModel::with('OrderShoplist.Goods')->where($map)->order('id desc')->paginate(10,false,array('query' => $this->request->param()));
        $data = [
            'list' => $list,
            'count' => $count,
        ];
        $this->assign($data);
        $this->setMeta($title);
    }


    public function createMap(){
      $map['company_id'] = ['gt', 0];
        input('tel') ? $map['tel'] = array('like', '%'.input('tel').'%') : '';
        input('username') ? $map['user_name'] = array('like', '%'.input('username').'%') : '';
        input('company_id') ? $map['company_id'] = input('company_id') : '';
        input('pay') ? $map['pay'] = input('pay') : '';
        $map['buystatus'] = array('gt','-1');
        if(input('start') && input('end')){
            $map['create_time'] = array('between time', [input('start'), input('end')]);
            $map['buystatus'] = array('gt','0');
        }
        input('buystatus') ? $map['buystatus'] = input('buystatus') : '';


        if(getGroupId()==6){
          $map['rebate_code'] = session('user_auth.uid');
        }
        return $map;
    }


      public function del() {
          $this->setOrderStatus(-1, '删除');
    	}
      //接单
      public function accept(){
          $this->setOrderStatus(2, '接单');
      }
      //发货
      public function deliver(){
        $this->setOrderStatus(3, '发货');
      }
      //确定收获
      public function take(){
        $this->setOrderStatus(4, '确定收货');
      }
      public function setOrderStatus($status = null, $text = '操作'){
        if($status === null){
          $this->error('参数错误');
        }
        $id = $this->getIds();
        $this->errorInfo($id);


        $result = OrderModel::where(['id' => ['IN', $id]])->update(['buystatus'=>$status]);

        $this->resultJudgeInfo($result, $text);
      }

      public function edit($id) {
        $this->errorInfo($id);
    		if (IS_POST) {
    			$result = model('Order')->change();
    			return $this->success("编辑成功！", Cookie('__forward__'));
    		} else {
          $info = model('Order')->get($id);
          $shoplist = model('OrderShoplist')->all(['order_id'=>$id]);

    			$this->assign('info',$info);
    			$this->assign('shoplist',$shoplist);
    			$this->setMeta('订单详情');
    			return $this->fetch('order/edit');
    		}
    	}

      public function printqiye($company_id = null){
        $id = $this->getIds();
        $this->errorInfo($id, '请选择订单');
        $this->errorInfo($company_id);
        $list =  OrderModel::where(['id' => ['IN', $id]])->select();
        $this->assign('list',$list);
        $this->setMeta('打印企业订单');
        return $this->fetch();
      }


      /**
       * 打印页面
       * @return [type] [description]
       */
      public function prints($id = ''){
        if (!$id) {
    			return $this->error("非法操作！");
    		}
        $info = OrderModel::get($id);
        $shoplist = OrderShoplist::all(['order_id'=>$id]);
    		$this->assign('info',$info);
    		$this->assign('shoplist',$shoplist);
    		$this->setMeta('订单详情');
    		return $this->fetch('order/prints');
      }

      /**
       * 打印页面
       * @return [type] [description]
       */
      public function simpleprints($id = ''){
        if (!$id) {
    			return $this->error("非法操作！");
    		}
        $info = OrderModel::get($id);
        $shoplist = OrderShoplist::all(['order_id'=>$id]);
    		$this->assign('info',$info);
    		$this->assign('shoplist',$shoplist);
    		$this->setMeta('订单详情');
    		return $this->fetch('order/simpleprints');
      }

}
